package com.owdp.code.controller;

import com.owdp.code.entity.User;
import com.owdp.code.service.UserService;
import com.owdp.code.utils.MD5Utils;
import com.owdp.code.utils.RandomValidateCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping(value = "/User")
public class UserConntroller {
    @Resource
    UserService userService;
    /*
    先获取到验证码，存入到cookie
     */
    @GetMapping(value = "/getcode")
    public RandomValidateCode getLoginSecurityCode(HttpServletResponse response, HttpServletRequest request){
        response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma","No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("Set-Cookie","name=value;HttpOnly");//设置HttpOnly属性,防止Xss攻击
        response.setDateHeader("Expire",0);
        RandomValidateCode randomValidateCode=new RandomValidateCode();
        try {
            randomValidateCode.getRandcode(request,response,"imagecode");// 输出图片方法
        }catch (Exception e){
            e.printStackTrace();
        }
        return randomValidateCode;
    }

    @GetMapping(value ="/login")
    public String login(String account,
                        String userpassword,
                        String securityCode,
                        HttpServletRequest request
    ) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String userPwd=userService.login(account);//获取数据库的密码
        String code = null;
        //1:获取cookie里面的验证码信息
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("imagecode".equals(cookie.getName())) {
                code = cookie.getValue();
                break;
            }
        }
        code=code.toLowerCase();//转换成小写
        securityCode=securityCode.toLowerCase();//转换成小写
        if (securityCode.equals(code)) {//先判断验证码是否正确在执行账号密码验证
            boolean decodePwd = MD5Utils.validpassword(userpassword, userPwd);//通过MD5Utiles工具解密码,判断true or false
//            System.out.println("account:"+account);
//            System.out.println("userpassword:"+userpassword);
//            System.out.println("decodePwd:"+decodePwd);
//            System.out.println("userPwd:"+userPwd);
            if (decodePwd) {
                return "登录成功";
            }
            return "登录失败，账号或密码错误";
        }
        return "登录失败，验证码错误";
    }

    @PostMapping(value="/regist")
    public String regist(User user,String idcard,String cpnumber) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String s=user.getUserpassword();//取出密码
        String userpassword=MD5Utils.getEncryptedPwd(s);//加密密码
        int c=userService.findUserByIdcardANDCpnumber(idcard, cpnumber);
        if (c==0){
            if(user.getCid()==1){
                user.setLpnumber(null);
                user.setUserpassword(userpassword);//把加密密码注入user
                userService.regist(user);
                return "手机号:"+cpnumber+"的乘客，恭喜您注册成功";
                //备注：需要通过前端来控制乘客不需输入车牌
            }else {
                user.setUserpassword(userpassword);//把加密密码注入user
                userService.regist(user);

                return "车牌号:"+user.getLpnumber()+"的车主,恭喜您注册成功";
                //备注：还需要通过前端来控制车主必须输入车牌
            }

        }
        return "身份证或者手机号已被注册";
    }
}
